package controllers

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"report/reportdb"
)

//TBD: I think it should be remove as I add http basic auth
func Login(response http.ResponseWriter, request *http.Request, _ httprouter.Params) {
	// return the username.
	//	auth := request.Header.Get("Authorization")
	//	fmt.Println(auth)
	//	token := strings.Split(auth," ")[1]
	name := request.FormValue("username")
	password := request.FormValue("password")
	fmt.Println("name:", name, " pwd:", password)
	err := reportdb.Login(name, password)
	if err != nil {

		fmt.Println("ERror....", err)
		response.WriteHeader(http.StatusInternalServerError)
		response.Write([]byte(err.Error()))
		return
	}

	fmt.Println("Success....")
	response.WriteHeader(http.StatusOK)
	response.Write([]byte("Hi there"))
}

func ListAllProducts(response http.ResponseWriter, request *http.Request, _ httprouter.Params) {
	result, err := reportdb.GetAllProducts()
	if err != nil {
		panic(err.Error())
		response.WriteHeader(http.StatusInternalServerError)
		response.Write([]byte(err.Error()))
		return
	}
	response.WriteHeader(http.StatusOK)
	response.Write(result)
}
